home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / mkfifo / mkfifo2.c < prev    next >
C/C++ Source or Header  |  1997-08-19  |  4KB  |  147 lines

  1. /*
  2. ** ****************************************************************************
  3. ** This implements the mkfifo command under OS/2.
  4. ** (c) Klaus Gebhardt, 1997
  5. ** ****************************************************************************
  6. */
  7.  
  8. /*
  9. ** ****************************************************************************
  10. ** This was written for the OS/2 port of Octave, but it is not part of Octave!
  11. ** You can use the code UNMODIFIED. If you think changes are necessary,
  12. ** please send me a mail (gebhardt@crunch.ikp.physik.th-darmstadt.de).
  13. ** Thanks,
  14. **   Klaus Gebhardt
  15. ** ****************************************************************************
  16. */
  17.  
  18. #include "mkfifo.h"
  19. #include "getopt.h"
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24.  
  25. static int delete_nmp_p    = 0;
  26. static int exit_server_p   = 0;
  27. static int list_nmp_p      = 0;
  28. static char *mode_string   = NULL;
  29.  
  30. /*
  31. ** Structure describing the options that mkfifo accepts.  We pass this
  32. ** structure to getopt_long ().  If you add or otherwise change this
  33. ** structure, you must also change the string which follows it.
  34. */
  35.  
  36. static struct option long_options[] =
  37. {
  38.   { "delete",  0, 0, 'd' },
  39.   { "exit",    0, 0, 'x' },
  40.   { "help",    0, 0, 'h' },
  41.   { "list",    0, 0, 'l' },
  42.   { "mode",    1, 0, 'm' },
  43.   { "version", 0, 0, 'v' },
  44.   {NULL, 0, NULL, 0}
  45. };
  46.  
  47. /* String describing the shorthand versions of the long options found above. */
  48. static char *short_options = "m:dxhlv?";
  49.  
  50.  
  51. static void print_help ()
  52. {
  53.   fprintf (stderr, "\nmkfifo for OS/2, Version 1.0\n"
  54.        "usage: mkfifo [options] named_pipe ...\n\n"
  55.        "[options]       --delete,  -d\n"
  56.        "                --exit,    -x\n"
  57.        "                --help,    -h\n"
  58.        "                --list,    -l\n"
  59.        "                --mode,    -m\n"
  60.        "                --version, -v\n");
  61.   exit (0);
  62. }
  63.  
  64.  
  65. static void print_version ()
  66. {
  67.   fprintf (stderr, "\nmkfifo for OS/2, Version 1.0");
  68.   exit (0);
  69. }
  70.  
  71.  
  72. static void usage ()
  73. {
  74.   fprintf (stderr, "\nmkfifo for OS/2, Version 1.0\n"
  75. "usage: mkfifo [options] named_pipe ...\nwith the folloing options:\n"
  76. "--delete, -d, --exit, -x, --help, -h, --list, -l, --mode, -m, --version, -v");
  77.   exit (0);
  78. }
  79.  
  80.  
  81. int main (int argc, char *argv[])
  82. {
  83.   int getopt_long_index;
  84.   int option_character;
  85.   int rc;
  86.  
  87.   while (1)
  88.     {
  89.       option_character = getopt_long
  90.         (argc, argv, short_options, long_options, &getopt_long_index);
  91.  
  92.       if (option_character == EOF) break;
  93.  
  94.       if (option_character == 0 && long_options[getopt_long_index].flag == 0)
  95.         option_character = long_options[getopt_long_index].val;
  96.  
  97.       switch (option_character)
  98.         {
  99.         case 0:
  100.           break;
  101.  
  102.         case 'd':
  103.       delete_nmp_p = 1;
  104.           break;
  105.  
  106.         case 'h':
  107.       print_help ();
  108.           break;
  109.  
  110.         case 'l':
  111.           list_nmp_p = 1;
  112.           break;
  113.  
  114.         case 'v':
  115.       print_version ();
  116.           break;
  117.  
  118.         case 'x':
  119.       exit_server_p = 1;
  120.           break;
  121.  
  122.         case 'm':
  123.       if (mode_string)  free (mode_string);
  124.           mode_string = strdup (optarg);
  125.           break;
  126.  
  127.         default:
  128.           usage ();
  129.         }
  130.     }
  131.  
  132.   if ((optind == argc) && !list_nmp_p && !exit_server_p)
  133.     usage ();
  134.  
  135.   while (optind != argc)
  136.     {
  137.       if (delete_nmp_p)  rc = remove_mkfifo (argv[optind++]);
  138.       else               rc = mkfifo (argv[optind++], 0);
  139.     }
  140.  
  141.   if (list_nmp_p)     rc = list_named_pipes ();
  142.  
  143.   if (exit_server_p)  rc = exit_server ();
  144.  
  145.   return 0;
  146. }
  147.